<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains the Profiler class.
 *
 * PHP version 5
 *
 * This file is part of Majic - A rapid-development web application framework
 * for PHP 5.
 *
 * Majic is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * Majic is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Majic. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     Profiler
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2007-2008 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: Profiler.php 34 2008-07-19 04:50:53Z mjijackson $
 */

/**
 * Helps with application profiling.
 *
 * @package     Profiler
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 */
class Profiler
{

    /**
     * Contains markers that represent points in time in the application
     * execution.
     *
     * @var     array
     * @access  private
     */
    private $markers = array();

    /**
     * Constructor.
     *
     * @access  private
     */
    private function __construct() {}

    /**
     * Gets the instance of the Profiler class.
     *
     * @return  Profiler    The Profiler singleton object
     * @access  public
     * @static
     */
    public static function getInstance()
    {
        static $inst;
        if (!$inst) {
            $inst = new self();
        }
        return $inst;
    }

    /**
     * Sets a time marker in the profiler.
     *
     * @param   string  $time   The name of the marker to set
     * @return  void
     * @access  public
     */
    public function mark($mark)
    {
        $this->markers[$mark] = array_sum(explode(' ', microtime()));
    }

    /**
     * Gets the amount of memory allocated to PHP (in MB).
     *
     * @return  float       The amount of memory used
     * @access  public
     */
    public function memoryUsage()
    {
        $memory = 0;
        if (function_exists('memory_get_usage')) {
            $memory = round(memory_get_usage() / 1024 / 1024, 2);
        }

        return $memory;
    }

    /**
     * Gets the amount of time that elapsed between two markers.
     *
     * @param   string  $start      The name of the starting marker
     * @param   string  $end        The name of the ending marker
     * @param   int     $precision  The amount of precision in the return value
     * @return  float               The amount of time between two markers
     * @access  public
     */
    public function timeElapsed($start, $end, $precision = 4)
    {
        if (!isset($this->markers[$start])) {
            return 0;
        }
        if (!isset($this->markers[$end])) {
            $this->mark($end);
        }

        return number_format($this->markers[$end] - $this->markers[$start], $precision);
    }

    /**
     * Dumps the profiler's information.
     *
     * @return  string      The profiler's information
     * @access  public
     */
    public function dump()
    {

    }

}

?>
